<?php
/**
 * Created by PhpStorm.
 * User: 庆金
 * Date: 2019-12-13
 * Time: 20:10
 */
$server = new Swoole\WebSocket\Server("0.0.0.0", 8812);
$server->set([
    'enable_static_handler' => true,
    'document_root' =>  '/var/www/html/swoole/data',
]);
//$server->set([]);
$server->on('open', "onOpen");

//监听ws链接打开事件
function onOpen(Swoole\WebSocket\Server $server, $request) {
    print_r($request->fd);
}

//监听ws消息时间
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    //向客户端推送数据
    $server->push($frame->fd, "this is server");
});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();